#if ( !defined __WEBPAGE_HUNTER_HPP__ )
#define __WEBPAGE_HUNTER_HPP__

/** webpage_hunter.hpp
  *		get a web page over internet.
  *
  *
  */

#include <string>
#include <vector>
#include <map>
#include <ostream>

#include <boost/asio.hpp>

using namespace std;

typedef map< string, string > HttpHeaderPairs;

class WebpageHunter
{
public:
	WebpageHunter ( boost::asio::io_service& io_service, ostream* page_header, ostream* page_content, const string& server, const string& port, const string& host, const string& path, HttpHeaderPairs& headers, signed int timeout_seconds=60 );
	~WebpageHunter ();

protected:
	void generate_request ( ostream& request, const string& host, const string& path,  const HttpHeaderPairs& headers);
	void handle_resolve ( const boost::system::error_code& err, boost::asio::ip::tcp::resolver::iterator endpoint_iterator );
	void handle_connect ( const boost::system::error_code& err, boost::asio::ip::tcp::resolver::iterator endpoint_iterator );
	void handle_write_request ( const boost::system::error_code& err );
	void handle_read_status_line ( const boost::system::error_code& err );
	void handle_read_headers ( const boost::system::error_code& err );
	void handle_read_content ( const boost::system::error_code& err );

	void handle_timeout ( const boost::system::error_code& err );

private:
	boost::asio::ip::tcp::resolver _resolver;
	boost::asio::ip::tcp::socket _socket;
	boost::asio::streambuf _request;
	boost::asio::streambuf _response;
	boost::asio::deadline_timer _deadtimer;
	istream* _response_stream;

	ostream* _header_output;
	ostream* _content_output;

	int _deadseconds;
};

#endif // __WEBPAGE_HUNTER_HPP__